package mn.idax.exchange.otc.config;

import mn.idax.exchange.otc.constant.CommonConstant;
import mn.idax.exchange.otc.service.impl.RedisService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * @Auther: dison
 * @Date: 2018/10/17 09:52
 * @Description:
 */
@Configuration
public class RedisSubListenerConfig {

    //初始化监听器
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                            MessageListenerAdapter listenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(listenerAdapter, new PatternTopic(CommonConstant.PUSH_NOTIFY_TOPIC));
        return container;
    }

    //利用反射来创建监听到消息之后的执行方法
    @Bean
    MessageListenerAdapter listenerAdapter(RedisService redisReceiver) {
        return new MessageListenerAdapter(redisReceiver, "receiveNotifyMessage");
    }
}
